package net.adetech.crj.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import net.adetech.crj.bean.ZggmtbForm;
import net.adetech.crj.bean.ZggmtbResult;
import net.adetech.crj.dao.ApplyMapper;
import net.adetech.crj.dao.RemindMapper;
import net.adetech.crj.entity.Apply;
import net.adetech.crj.entity.Remind;
import net.adetech.crj.entity.User;
import net.adetech.crj.entity.tb.*;
import net.adetech.crj.utils.ApiUtils;
import net.adetech.crj.utils.InnerServiceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service("zggmtb.check")
public class ZggmtbCheckService implements BaseService {

    @Autowired
    ApiUtils apiUtils;
    @Autowired
    ApplyMapper applyMapper;

    @Autowired
    RemindMapper remindMapper;


    @Override
    public Map<String, Object> invoke(Map<String, Object> params) {
        String sqh = null;
        Map<String, Object> resultMap = new HashMap<String, Object>();
        ZggmtbForm form = (ZggmtbForm) params.get("form");
        User user = (User) params.get("user");
        if (user==null)
        {
            user = new User();
            user.setIdNumber("check");
        }
        String result = "ok";
        String msg = "";
        List<Apply> applyList = applyMapper.getApplyListByIdNumberAndApplyUserId(form.getSfzh(), user.getIdNumber());


        if (!this.checkOtherProvinceState(form)) {
            Remind remind = remindMapper.getRemindByCode("E0101");
            result = "error";
            msg = remind.getContent();
            return InnerServiceUtil.buildResult("result", result, "msg", msg);
        }


        if (applyList.size() > 0) {
            result = "tips";
            msg = "系统中已存在您的申请信息,继续提交将覆盖原有申请信息内容!";
            return InnerServiceUtil.buildResult("result", result, "msg", msg);
        }

        return InnerServiceUtil.buildResult("result", result, "msg", msg);


    }

    /**
     * 检查外省国家工作人员
     *
     * @param form 填表信息
     * @return 检查结果, 通过为true 不通过为false
     */
    public boolean checkOtherProvinceState(ZggmtbForm form) {
        String sfzh = form.getSfzh();
        String hkszd = form.getHkszd();
        //判断是否山东省外户口
        if (hkszd.indexOf("37") != 0) {
            JSONObject officialResult = apiUtils.queryBySfzh("kz_queryOfficial", sfzh);
            //查询是否国家工作人员
            if (officialResult.getInteger("success") == 1) {
                //非单独签注
                if (form.getSqlb() != null && form.getSqlb().size() > 0) {
                    return false;
                }
                //申请香港非商务签注
                if (form.getQwd().contains("HKG")) {
                    if (!form.getQzzlHkg().equals("13")) {
                        return false;
                    }
                }
                //申请澳门非商务签注
                if (form.getQwd().contains("MAC")) {
                    if (!form.getQzzlMac().equals("13")) {
                        return false;
                    }
                }
                //申请台湾非应邀签注
                if (form.getQwd().contains("TWN")) {
                    if (!form.getQzzlTwn().equals("22")) {
                        return false;
                    }
                }


            }
        }
        return true;
    }


}
